package com.gjw.planeWar;


import com.gjw.style.Style;
import lombok.Data;
import lombok.Getter;
import utils.EnumUtil;

import java.awt.*;
import java.util.Objects;
import java.util.Random;

/**
 * @author gjw19
 * @date 2019/5/2
 * 敌军飞机类
 */
@Data
public class EnemyPlane extends GameObject {
    // 敌军飞机水平速度
    public static int HORIZONTAL_SPEED = 2;
    // 敌军飞机下降速度
    public int downSpeed;
    // 英雄飞机方向
    private EnemyDir enemyDir = EnemyDir.STOP;
    // 敌军飞机大小
    private static Style style = Style.getINSTANCE();
    @Getter
    public static int WIDTH = style.enemyPlane.getWidth();
    @Getter
    public static int HEIGHT = style.enemyPlane.getHeight();

    private boolean living = true;
    private GameModel gm = GameModel.getInstance();
    Rectangle rect = new Rectangle();
    private static final Props props = Props.getInstance();

    public EnemyPlane(int x, int y, EnemyDir enemyDir, int downSpeed) {
        this.x = x;
        this.y = y;
        this.enemyDir = enemyDir;
        this.downSpeed = downSpeed;

        rect.x = this.x;
        rect.y = this.y;
        rect.width = WIDTH;
        rect.height = HEIGHT;

        gm.add(this);
        gm.enemyPlaneNums++;
    }

    // 敌军飞机画的方法
    @Override
    public void paint(Graphics g) {
        if (!living) {
            gm.remove(this);
            gm.enemyPlaneNums--;
        }
        g.drawImage(style.enemyPlane, x, y, null);
        move();
    }

    // 移动方法
    private void move() {
        y += downSpeed;

        // 敌军飞机随机移动
        Random random = new Random();
        if (random.nextInt(11) > props.CHANGE_NUM) {
            enemyDir = EnumUtil.randomEnum(EnemyDir.class);
        }

        // 根据按下的按钮进行移动
        switch (enemyDir) {
            case LEFT:
                x -= HORIZONTAL_SPEED;
                break;
            case RIGHT:
                x += HORIZONTAL_SPEED;
                break;
        }
        // 超出边界，该子弹不存活
        if (y > props.GAME_HEIGHT) {
            this.die();
        }
        // 敌军飞机边界检测，只检测横向的
        if (x < 0) {
            x = 0;
        }
        if (x > props.GAME_WIDTH - EnemyPlane.getWIDTH()) {
            x = props.GAME_WIDTH - EnemyPlane.getWIDTH();
        }
        rect.x = this.x;
        rect.y = this.y;
        // 未移动直接返回
        if (Objects.equals(enemyDir, EnemyDir.STOP)) {
            return;
        }
    }

    public void die() {
        living = false;
    }

}
